package com.seaboxdata.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

/**
 * @author
 * @create 2020-08-27 15:25
 **/
@Slf4j
public class TransRealHttpsHandler {

    public static List sendPost(String url, String token, Class<?> cls) {
        HttpClient client = null;
        HttpPost request;
        List<Object> results = new ArrayList<>();
        try {
            client = new SSLClient();
            request = new HttpPost(new URI(url));
            JSONObject jsonParam = new JSONObject();
            StringEntity s = new StringEntity(jsonParam.toString(), "utf-8");
            s.setContentType("application/json");
            request.setEntity(s);
            if (token != null) {
                request.setHeader("X-API-KEY", token);
            }
            HttpResponse response = client.execute(request);
            int returncode = response.getStatusLine().getStatusCode();
            if (returncode == 200) {
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                JSONArray speeds = null;
                if (JSONObject.parse(result) instanceof JSONArray) {
                    speeds = (JSONArray) JSONObject.parse(result);
                    Iterator<Object> iterator = speeds.iterator();
                    while (iterator.hasNext()) {
                        JSONObject next = (JSONObject) iterator.next();
                        Object t = next.toJavaObject(cls);
                        results.add(t);
                    }
                } else if (JSONObject.parse(result) instanceof JSONObject) {
                    JSONObject nextResult = (JSONObject) JSONObject.parse(result);
                    speeds = (JSONArray) JSONObject.parse(nextResult.getString("data"));
                    Iterator<Object> iterator = speeds.iterator();
                    while (iterator.hasNext()) {
                        JSONObject next = (JSONObject) iterator.next();
                        Object t = next.toJavaObject(cls);
                        results.add(t);
                    }
                } else {
                    log.error("invalid data,result:{}", result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return results;
    }

    public static List sendGet(String url, String token, Class<?> cls) {
        HttpClient client = null;
        HttpGet request;
        List<Object> results = new ArrayList<>();
        try {
            client = new SSLClient();
            request = new HttpGet(new URI(url));
            request.setHeader("content-type","application/json");
            if (token != null) {
                request.setHeader("X-API-KEY", token);
            }
            HttpResponse response = client.execute(request);
            int returncode = response.getStatusLine().getStatusCode();
            if (returncode == 200) {
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                JSONArray speeds = null;
                if (JSONObject.parse(result) instanceof JSONArray) {
                    speeds = (JSONArray) JSONObject.parse(result);
                    Iterator<Object> iterator = speeds.iterator();
                    while (iterator.hasNext()) {
                        JSONObject next = (JSONObject) iterator.next();
                        Object t = next.toJavaObject(cls);
                        results.add(t);
                    }
                } else if (JSONObject.parse(result) instanceof JSONObject) {
                    JSONObject nextResult = (JSONObject) JSONObject.parse(result);
                    speeds = (JSONArray) JSONObject.parse(nextResult.getString("data"));
                    Iterator<Object> iterator = speeds.iterator();
                    while (iterator.hasNext()) {
                        JSONObject next = (JSONObject) iterator.next();
                        Object t = next.toJavaObject(cls);
                        results.add(t);
                    }
                } else {
                    log.error("invalid data,result:{}", result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return results;
    }



    public static List sendPostParam(String url, String token,JSONObject jsonparam, Class<?> cls) {
        HttpClient client = null;
        HttpPost request;
        List<Object> results = new ArrayList<>();
        try {
            client = new SSLClient();
            request = new HttpPost(new URI(url));
            JSONObject jsonParam = new JSONObject();
            StringEntity s = new StringEntity(jsonParam.toString(), "utf-8");
            s.setContentType("application/json");
            request.setEntity(s);
            if (token != null) {
                request.setHeader("X-API-KEY", token);
            }
            HttpResponse response = client.execute(request);
            int returncode = response.getStatusLine().getStatusCode();
            if (returncode == 200) {
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                JSONArray speeds = null;
                if (JSONObject.parse(result) instanceof JSONArray) {
                    speeds = (JSONArray) JSONObject.parse(result);
                    Iterator<Object> iterator = speeds.iterator();
                    while (iterator.hasNext()) {
                        JSONObject next = (JSONObject) iterator.next();
                        Object t = next.toJavaObject(cls);
                        results.add(t);
                    }
                } else if (JSONObject.parse(result) instanceof JSONObject) {
                    JSONObject nextResult = (JSONObject) JSONObject.parse(result);
                    speeds = (JSONArray) JSONObject.parse(nextResult.getString("data"));
                    Iterator<Object> iterator = speeds.iterator();
                    while (iterator.hasNext()) {
                        JSONObject next = (JSONObject) iterator.next();
                        Object t = next.toJavaObject(cls);
                        results.add(t);
                    }
                } else {
                    log.error("invalid data,result:{}", result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return results;
    }

    /**
     * 调用HTTPCLIENT POST请求
     *
     * @param url
     * @param headers
     * @return
     * @throws Exception
     */
    public static String sendPost(String url, Map<String, String> headers) throws Exception {
        HttpClient client = null;
        HttpPost request;
        HttpResponse response = null;
        String result = "";
        try {
            client = new SSLClient();
            request = new HttpPost(new URI(url));
            JSONObject jsonParam = new JSONObject();
            StringEntity s = new StringEntity(jsonParam.toString(), "utf-8");
            s.setContentType("application/json");
            request.setEntity(s);
            if (headers != null) {
                for (Entry<String, String> entry : headers.entrySet()) {
                    request.setHeader(entry.getKey(), entry.getValue());
                }
            }
            response = client.execute(request);
            if (response != null) {
                int returncode = response.getStatusLine().getStatusCode();
                if (returncode == 200) {
                    result = EntityUtils.toString(response.getEntity(), "UTF-8");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return result;
    }

    /**
     * 获取HTTPCLIENT结果集,转换成JAVABEAN的LIST
     *
     * @param result
     * @param cls
     * @return
     */
    public static List parseJSONObject(String result, Class<?> cls) {
        List<Object> results = new ArrayList<>();
        JSONArray speeds = null;
        if (JSONObject.parse(result) instanceof JSONArray) {
            speeds = (JSONArray) JSONObject.parse(result);
            Iterator<Object> iterator = speeds.iterator();
            while (iterator.hasNext()) {
                JSONObject next = (JSONObject) iterator.next();
                Object t = next.toJavaObject(cls);
                
                results.add(t);
            }
        } else if (JSONObject.parse(result) instanceof JSONObject) {
            JSONObject next = (JSONObject) JSONObject.parse(result);
            Object t = next.toJavaObject(cls);
            results.add(t);
        } else {
            log.error("invalid data,result:{}", result);
        }
        return results;
    }

}
